package com.marchsoft.service.impl;

import com.marchsoft.common.status.ActivityStatu;
import com.marchsoft.dao.ActivityMapper;
import com.marchsoft.dao.StudentMapper;
import com.marchsoft.dao.TeacherMapper;
import com.marchsoft.domain.Activity;
import com.marchsoft.domain.Student;
import com.marchsoft.domain.Teacher;
import com.marchsoft.service.ITeacherService;
import com.marchsoft.vo.TeaOwnInfo;
import com.marchsoft.vo.TeacherExtra;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements ITeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ActivityMapper activityMapper;

    @Override
    public List<Map> queryGroupAll(Integer sid) {
        List<Teacher> teaList = teacherMapper.selectAll();
        List<TeacherExtra> teacherExtras = teacherMapper.queryTeaTeamPeoples();
//        Student student = studentMapper.selectByPrimaryKey(sid);
        Activity activity = new Activity();
        activity.setSid(sid);
        activity.setStatus(ActivityStatu.STUDENT_APPLYING_TEACHER);
        activity.setIsDelete(1);
        activity = activityMapper.selectOne(activity);
        List<Map> teachers = new ArrayList<>();
        for (int i = 0; i < teaList.size(); i++) {
            Map<String, Object> teacher = new HashMap<>();
            teacher.put("tid", teaList.get(i).getTid());
            teacher.put("tname", teaList.get(i).getName());
            if (activity != null && activity.getTid().equals(teaList.get(i).getTid())) {
                teacher.put("status", 2);
            } else {
                for (int j = 0; j < teacherExtras.size(); j++) {
                    if (teaList.get(i).getTid().equals(teacherExtras.get(j).getTid())) {
                        if (teacherExtras.get(j).getStuNumber() >= 10) {
                            teacher.put("status", 3);
                        } else {
                            teacher.put("status", 1);
                        }
                    } else {
                        teacher.put("status", 1);
                    }
                }
            }
            teachers.add(teacher);
        }
        System.out.println(123);
        return teachers;
    }

    @Override
    public Map<String,Object> queryTeacherInfoById(Integer tid,Integer sid) {
        Map<String, Object> teacher = new HashMap<>();
        Teacher tea = teacherMapper.selectByPrimaryKey(tid);
        System.out.println(456);
        TeacherExtra teacherExtra = teacherMapper.queryTeaTeamPeople(tid);
        Integer members =null;
        if(teacherExtra.getStuNumber()==null) {
            members =0;
        } else{
            members = teacherExtra.getStuNumber();
        }
        Activity activity = new Activity();
        activity.setSid(sid);
        activity.setTid(tid);
        activity.setStatus(ActivityStatu.STUDENT_APPLYING_TEACHER);
        activity.setIsDelete(1);
        activity = activityMapper.selectOne(activity);

        teacher.put("tid", tid);
        teacher.put("name", tea.getName());
        teacher.put("major", tea.getMajor());
        teacher.put("email", tea.getEmail());
        teacher.put("members", members);
        Integer applicationStatus = null;
        if (activity != null) {
            applicationStatus = 2;
        }else if (members>=10){
            applicationStatus =3;
        }else {
            applicationStatus =1;
        }
        teacher.put("ApplicationStatus", applicationStatus);
        System.out.println(teacher);
        return teacher;
    }

    @Override
    public List<Object> queryTeacherTeam(Integer tid) {
        List<Object> list = new ArrayList<>();
        Teacher teacher = new Teacher();
        teacher.setTid(tid);
        teacher = teacherMapper.selectOne(teacher);
        Student student = new Student();
        student.setTid(tid);
        List<Student> students = studentMapper.select(student);
        list.add(teacher);
        list.add(students);
        return list;
    }

    @Override
    public void submitApplications(Integer sid, Integer tid) {
        Activity activity = new Activity();
        activity.setSid(sid);
        activity.setTid(tid);
        activity.setStatus(ActivityStatu.STUDENT_APPLYING_TEACHER);
        activityMapper.insertSelective(activity);
    }

    @Override
    public void submitChange(Integer sid, Integer tragetTid, Integer currentTid) {
        Activity quitCurrentTeacher = new Activity();
        quitCurrentTeacher.setSid(sid);
        quitCurrentTeacher.setTid(currentTid);
        quitCurrentTeacher.setStatus(ActivityStatu.STUDENT_EXITING_TEACHER);
        quitCurrentTeacher.setATime(new java.sql.Timestamp(System.currentTimeMillis()));
        Example example = new Example(Activity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tid", currentTid);
        criteria.andEqualTo("sid", sid);
        activityMapper.updateByExample(quitCurrentTeacher, example);
        example = null;
        criteria = null;
        Activity applyCurrentTeacher = new Activity();
        applyCurrentTeacher.setSid(sid);
        applyCurrentTeacher.setTid(tragetTid);
        applyCurrentTeacher.setStatus(ActivityStatu.STUDENT_APPLYING_TEACHER);
        applyCurrentTeacher.setATime(new java.sql.Timestamp(System.currentTimeMillis()));
        List<Activity> targetTeahcerIsNull = new ArrayList<>();
        example = new Example(Activity.class);
        criteria = example.createCriteria();
        criteria.andEqualTo("tid", tragetTid);
        criteria.andEqualTo("sid", sid);
        targetTeahcerIsNull = activityMapper.selectByExample(example);
        example = null;
        criteria = null;
        if (targetTeahcerIsNull == null) {
            activityMapper.insertSelective(applyCurrentTeacher);
        } else {
            example = new Example(Activity.class);
            criteria = example.createCriteria();
            criteria.andEqualTo("tid", tragetTid);
            criteria.andEqualTo("sid", sid);
            activityMapper.updateByExample(applyCurrentTeacher, example);

        }
    }

    @Override
    public TeaOwnInfo queryOwnInfo(Integer tid) {
        return teacherMapper.queryOwnInfo(tid);
    }

}
